package cn.edu.qzuie.artist.module.td.controller.admin.school;

import cn.edu.qzuie.artist.module.td.api.school.TdSchoolApi;
import cn.edu.qzuie.artist.module.td.api.school.dto.TdSchoolSelectRespDTO;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.school.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.school.SchoolDO;
import cn.edu.qzuie.artist.module.td.service.school.SchoolService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD学校")
@RestController
@RequestMapping("/td/school")
@Validated
public class SchoolController {

    @Resource
    private SchoolService schoolService;
    @Resource
    private TdSchoolApi tdSchoolApi;

    @PostMapping("/create")
    @Operation(summary = "创建学校")
    @PreAuthorize("@ss.hasPermission('td:school:create')")
    public CommonResult<Long> createSchool(@Valid @RequestBody SchoolSaveReqVO createReqVO) {
        return success(schoolService.createSchool(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学校")
    @PreAuthorize("@ss.hasPermission('td:school:update')")
    public CommonResult<Boolean> updateSchool(@Valid @RequestBody SchoolSaveReqVO updateReqVO) {
        schoolService.updateSchool(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学校")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:school:delete')")
    public CommonResult<Boolean> deleteSchool(@RequestParam("id") Long id) {
        schoolService.deleteSchool(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学校")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:school:query')")
    public CommonResult<SchoolRespVO> getSchool(@RequestParam("id") Long id) {
        SchoolDO school = schoolService.getSchool(id);
        return success(BeanUtils.toBean(school, SchoolRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学校分页")
    @PreAuthorize("@ss.hasPermission('td:school:query')")
    public CommonResult<PageResult<SchoolRespVO>> getSchoolPage(@Valid SchoolPageReqVO pageReqVO) {
        PageResult<SchoolDO> pageResult = schoolService.getSchoolPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchoolRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学校 Excel")
    @PreAuthorize("@ss.hasPermission('td:school:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSchoolExcel(@Valid SchoolPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchoolDO> list = schoolService.getSchoolPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学校.xls", "数据", SchoolRespVO.class,
                        BeanUtils.toBean(list, SchoolRespVO.class));
    }


    @GetMapping("/getSchoolsForSelect")
    @Operation(summary = "获得学校信息")
    @PreAuthorize("@ss.hasPermission('td:school:query')")
    public CommonResult<List<TdSchoolSelectRespDTO>> getSchools(){
        List<TdSchoolSelectRespDTO> list=tdSchoolApi.getSchool();
        return success(list);
    }


    @GetMapping("/getSchoolByRegionId")
    @Operation(summary = "根据地域编号RegionId获得学校信息")
    @PreAuthorize("@ss.hasPermission('td:school:query')")
    public  CommonResult<List<TdSchoolSelectRespDTO>> getSchoolByRegionId(Long regionId){
        List<TdSchoolSelectRespDTO> list=tdSchoolApi.getSchool(regionId);
        return success(list);
    }

}